from db_init import db,Model,session
from sqlalchemy import *
import datetime

#1.创建模型类

class Student(Model):
    """学生表模型"""
    #1.绑定字段信息
    __tablename__ = 'student'

    id = Column(Integer,primary_key=True)
    name = Column(String(100))
    gender = Column(String(10))
    birthday = Column(Date)
    add_time = Column(DateTime,default=datetime.datetime.now)
    orders = Column(SMALLINT)

    #2.创建关系属性



if __name__ == '__main__':
    #2.创建表
    # Model.metadata.create_all(engine)
    # 新增数据
    # student1 = Student(name='张二',gender='男',birthday=datetime.date(1990,1,1),orders=1)
    # student2 = Student(name='张四',gender='男',birthday=datetime.date(1990,1,1),orders=1)
    # student3 = Student(name='张五',gender='男',birthday=datetime.date(1990,1,1),orders=1)
    # student4 = Student(name='张六',gender='男',birthday=datetime.date(1990,1,1),orders=1)
    # session.add(student)#add --添加一条数据
    # session.add_all([student1,student2,student3,student4])#add_all --添加多条数据
    # session.commit()
    #
    # #3.查询数据
    # #查询一条数据
    # student = session.query(Student).get(1)#get(1) ---where id =1
    # students = session.query(Student).filter(Student.gender=='男').all()
    # for stu in students:
    #     print(stu.name)
    #get 获取一个|filter 获取多个

    # #查询所有数据
    # all_data = session.query(Student).filter(Student.gender=='男',Student.name=="张三").all()
    # 更新数据
    #1.查询要更改的数据（目录为了让orm实现表记录i和模型对象的映射）
    # student = session.query(Student).get(1)
    # # print(dict(vars(student)))#获取所有属性
    # student.name = '张三三'
    # session.commit()
    #2.update方法
    # session.query(Student).filter(Student.name=='张三三').update({'name':'张三'})
    # session.commit()
    # 删除数据
    session.query(Student).filter(Student.name=='张三').delete()
    session.commit()
    
